<?php

namespace log;

require_once(APP_ROOT . '/modules/log/logCommon.php');

/**
 * Logging system entry.
 */
class LogEntry
{
    /**
     * The timestamp when the log entry was created.
     */
    public $timestamp;

    /**
     * The hostname from where the log was created.
     */
    public $host;

    /**
     * The module name from where the log was created.
     */
    public $module;

    /**
     * The file name from where the log was created.
     */
    public $file;

    /**
     * The line number from where the log was created.
     */
    public $line;

    /**
     * The functio name from where the log was created.
     */
    public $function;

    /**
     * The system process identifier of the service.
     */
    public $procid;

    /**
     * The thread identifier if any.
     */
    public $threadid;

    /**
     * The system username executing the service.
     */
    public $sysusr;

    /**
     * Entry type identifier.
     * default: logTypeNotice
     */
    public $type;

    /**
     * Entry message.
     * @note must be a JSON compliant value.
     */
    public $msg;

    //--------------------------------------------------------------------------
    /**
     * Class constructor.
     */
    function __construct()
    {
        // Initialize class properties.
        $this->timestamp = time();
        $this->host      = null;
        $this->module    = null;
        $this->file      = null;
        $this->line      = null;
        $this->function  = null;
        $this->procid    = null;
        $this->threadid  = null;
        $this->sysusr    = null;
        $this->type      = null;
        $this->msg       = null;
    }
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    /**
     * Used to dump the current entry as string.
     * @return raw log entry.
     */
    public function dump()
    {
        $entry = ""; // Entry as string.

        // Generate entry.
        if(!(strpos($this->msg, "{") === 0) ||
           !(substr($this->msg, -strlen("}")) == "}")) {
            $this->msg = "{\"msg\":\"".$this->msg;
            $this->msg = $this->msg."\"}";
        }
        $entry .= "|";
        $entry .= $this->timestamp;
        $entry .= "|";
        $entry .= $this->host;
        $entry .= "|";
        $entry .= $this->module;
        $entry .= "|";
        $entry .= $this->file;
        $entry .= "|";
        $entry .= $this->line;
        $entry .= "|";
        $entry .= $this->function;
        $entry .= "|";
        $entry .= $this->procid;
        $entry .= "|";
        $entry .= $this->threadid;
        $entry .= "|";
        $entry .= $this->sysusr;
        $entry .= "|";
        $entry .= $this->type;
        $entry .= "|";
        $entry .= $this->msg;
        $entry .= "|";

        return $entry;
    }
    //--------------------------------------------------------------------------
}
?>
